package com.foton.lotteryapplication.mq;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.support.Acknowledgment;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static com.foton.lotteryapplication.mq.KafkaProducerTest.TOPIC_TEST;

/**
 * @Author RenPu
 * @Date 2024/1/8 11:28
 * @Version 1.0
 * @Description: kafka 消息消费者 -Test
 **/

@Slf4j
@Component
public class KafkaConsumerTest {

    @KafkaListener(topics = {KafkaProducerTest.TOPIC_TEST},groupId = KafkaProducerTest.TOPIC_GROUP)
    public void topicTest(ConsumerRecord<?, ?> record, Acknowledgment ack, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
        Optional<?> message = Optional.ofNullable(record.value());
        if (message.isPresent()) {
            Object msg = message.get();
            log.info(TOPIC_TEST+"消费者 ===topic_test 消费了： Topic:" + topic + ",Message:" + msg);
            ack.acknowledge();
        }
    }


}
